package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;
    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @Override
    public ResponseResult uploadImage(MultipartFile file) {

        try {
            if(file == null || file.getSize() == 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            //1.获取图片的后缀名
        String originalFilename = file.getOriginalFilename();//获取图片的原始名称 8.jpg
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //2.重新定义上传图片的名称，保证是唯一的
        String imageName= UUID.randomUUID().toString().replace("-","")+suffix;

        //3.执行上传图片到minio中，返回一个路径地址
            String path = fileStorageService.uploadImgFile("", imageName, file.getInputStream());
        //4.保存数据到wm_material表中
            WmMaterial wmaterial=new WmMaterial();
            wmaterial.setUserId(WmThreadLocalUtil.getWmUser().getId());
            wmaterial.setType((short) 0);//图片
            wmaterial.setIsCollection((short) 0);//全部
            wmaterial.setUrl(path);
            wmaterial.setCreatedTime(new Date());
            save(wmaterial);
         //5.返回数据
            return ResponseResult.okResult(wmaterial);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    /**
     * 查询图片列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //1.参数检查
        dto.checkParam();
        //2.判断是否是全部
        if(dto.getIsCollection()==null){
            dto.setIsCollection((short) 0);//全部
        }

        //3.分页查询素材列表
        IPage<WmMaterial> ipage=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper= Wrappers.lambdaQuery();

        if(dto.getIsCollection()==1){//收藏
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }

        //根据用户id查询
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getWmUser().getId());

        //根据时间倒叙展示
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //分页查询
        ipage=page(ipage,queryWrapper);

        //4.封装数据
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) ipage.getTotal());
        result.setData(ipage.getRecords());

        return result;
    }
}
